package HelloSeries.ui.prefs.elems.flextables.model;

import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.table.AbstractTableModel;

import org.apache.log4j.Logger;

import HelloSeries.metier.Serie;
import HelloSeries.metier.Version;

public class FlexTableSerieModel
    extends AbstractTableModel
    implements IFlexTableModel
{
	private Serie new_serie;
	private String[] entetes;
	private ArrayList<Serie> donnees;
	private static Logger logger = Logger.getLogger(FlexTableSerieModel.class);

	public FlexTableSerieModel(String[] entetes , ArrayList<Serie> donnees) {
		this.entetes = entetes;
		this.donnees = donnees;
	}

	public int getColumnCount() {
		return entetes.length;
	}

	public int getRowCount() {
		return donnees.size()+1;
	}

	public Object getValueAt(int row, int col) {
		if (row < donnees.size()){
			Serie s = donnees.get(row);
			switch (col){
    			case 0 : return s.isActive().equals("1");
    			case 1 : return new JTextField(s.getNomSerie(),15);
    			case 2 : return new JTextField(""+s.getSaison(),3);
    			case 3 : return new JTextField(""+s.getEpisode(),3);
    			case 4 : return s.isHD().equals("720p");
    			case 5 : return s.getVersion();
    			case 6 : return new JButton("x");
    			default : return null;
			}
		}
		else if (new_serie != null){
			switch (col){
    			case 0 : return new_serie.isActive().equals("1");
    			case 1 : return new JTextField(new_serie.getNomSerie(),15);
    			case 2 : return new JTextField(""+(new_serie.getSaison() == -1?"":new_serie.getSaison()),3);
    			case 3 : return new JTextField(""+(new_serie.getEpisode() == -1?"":new_serie.getEpisode()),3);
    			case 4 : return new_serie.isHD().equals("720p");
    			case 5 : return new_serie.getVersion();
    			case 6 : return new JButton("+");
    			default : return null;
			}
		}
		else {
			switch (col){
    			case 0 : return new Boolean(false);
    			case 1 : return new JTextField("",15);
    			case 2 : return new JTextField("",3);
    			case 3 : return new JTextField("",3);
    			case 4 : return new Boolean(false);
    			case 5 : return Version.VO;
    			case 6 : return new JButton("+");
    			default : return null;
			}
		}
	}

	public Class getColumnClass(int c) {
		if (getValueAt(0, c) != null){
			return getValueAt(0, c).getClass();
		}
		return Object.class;
	}


	public boolean isCellEditable(int row, int col) {
		return true;
	}

	public String getColumnName(int column) {
		return entetes[column];
	}

	public ArrayList<Serie> getDonnees() {
		return donnees;
	}

	public void setValueAt(Object value, int row, int col) {

		if (row < donnees.size()){
			Serie s = donnees.get(row);
			switch (col){
			case 0 :
				s.setActive((Boolean)value);
				break;
                case 1:
				s.setNomSerie((String) value);
				break;
                case 2:
				s.setSaison(Integer.parseInt(value.toString().trim()));
				break;
                case 3:
				s.setEpisode(Integer.parseInt(value.toString().trim()));
				break;
                case 4:
				s.sethd((Boolean)value);
				break;
                case 5:
				s.setVersion((Version) value);
				break;
			}
		}
		else   {
            if ( new_serie == null )
            {
                new_serie = new Serie();
                new_serie.setActive( false );
            }
			switch (col){
			case 0 :
				new_serie.setActive((Boolean)value);
				break;
                case 1:
				new_serie.setNomSerie(value.toString().trim());
				break;
                case 2:
				new_serie.setSaison(Integer.parseInt(value.toString().trim()));
				break;
                case 3:
				new_serie.setEpisode(Integer.parseInt(value.toString().trim()));
				break;
                case 4:
				new_serie.sethd((Boolean)value);
				break;
                case 5:
				new_serie.setVersion((Version) value);
				break;
			}
		}
		fireTableCellUpdated(row, col);
	}

	public void addRow() {
		String name = new_serie.getNomSerie();
		int episode = new_serie.getEpisode();
		int saison = new_serie.getSaison();

		if (!name.isEmpty() && episode> -1 &&  saison > -1){
			donnees.add(new_serie);
			fireTableRowsInserted(getRowCount(), getRowCount());
			new_serie = null;
		}
		else {
			String mess="";
			if (name.isEmpty()) {
				mess ="\"Nom\" ";
			}
			if (saison> -1) {
				mess+="\"Saison\" ";
			}
			if ( episode> -1) {
				mess+="\"Episode\" ";
			}
			raisePopUp("Le(s) champ(s) "+mess+"sont vides ligne ");

		}
	}

	public void removeRow(int deleted_row) {
		donnees.remove(deleted_row);
        fireTableRowsDeleted( deleted_row, deleted_row );
	}

	public void showListSeries(){
		for (Iterator iterator = donnees.iterator(); iterator.hasNext();) {
			Serie s = (Serie) iterator.next();
			logger.debug("donnees "+s);
		}
	}

	public void raisePopUp(String mess){
		JOptionPane.showMessageDialog(new JOptionPane(),mess,"Erreur",JOptionPane.ERROR_MESSAGE);
	}
}
